//Random number generator in the range [-1,1]
float randomNumberGenerator(int x, int y){
	int n = (int)(x * 40.0 + y * 6400.0);
	n = (n << 13) ^ n;
	return 1.0f - (float)( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f;
}



//Kernel that init the crowd uniformly in the area specified
/*
 *  0---------------------(area_size_x)
 *  |                      |
 *  |                      |
 *  |                      |
 *  |                      |
 *  (area_size_y)-----------
 */
kernel void initCrowd(	global float4 *agents,
						int agents_mesh_width,
						int area_size_x,
						int area_size_y,
						int area_size_z) {

    unsigned int x = get_global_id(0);
    unsigned int y = get_global_id(1);
    
    int numAgents = agents_mesh_width * agents_mesh_width;
    int particleID = x * y;
    
    if( particleID > numAgents)
    	return;
    	
    particleID = y*agents_mesh_width + x;
    
    int centerX = area_size_x / 2;
    int centerZ = area_size_z / 2;

	// calculate uv coordinates    
    float alpha = (360 * particleID / agents_mesh_width) * (3.14159265 / 180);
    float sinalpha = sin(alpha);
    float cosalpha = cos(alpha);

    float u = centerX + (cosalpha) * (centerX + (agents_mesh_width - y));
    float v = centerZ + (sinalpha) * (centerZ + (agents_mesh_width - y));
    float w = area_size_y - ((y % agents_mesh_width) * (area_size_y / (float)agents_mesh_width));

    // write output vertex
    agents[y*agents_mesh_width + x]   = (float4)(u, w , v, 1.0f);

}